package org.me.app.instructor;

import java.util.ArrayList;

import org.me.app.R;
import org.me.bean.Announcement;
import org.me.bean.Instructor;


import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class AnnListActivity extends ListActivity {
	private int request_Code = 1;

	ArrayList<org.me.bean.Announcement> annList = new ArrayList<org.me.bean.Announcement>();// Announcement
																							// //
																							// List
	Announcement ann = new Announcement();
	Instructor inst = new Instructor();
	String courseScheduleId = new String();

	org.me.services.WSlib ws = new org.me.services.WSlib();
	ArrayAdapter<Announcement> aaAnn;

	private static final int DIALOG_YES_NO_LONG_MESSAGE = 2;
	private static final int OtherDialog = 3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		Bundle b = getIntent().getExtras();
		
		inst = b.getParcelable("Instructor");
		courseScheduleId = b.getString("CourseScheduleId");

		// get announcement from server

		org.me.services.WSlib ws = new org.me.services.WSlib();
		annList = (ArrayList<Announcement>) ws
				.refreshAnnouncementList(courseScheduleId);

		if (annList.isEmpty()) {
			Toast.makeText(getApplicationContext(),
					"There is no announcement availiable yet!!!",
					Toast.LENGTH_LONG).show();

		}

		aaAnn = new ArrayAdapter<org.me.bean.Announcement>(
				getApplicationContext(), R.layout.courseinfor, annList);
		setListAdapter(aaAnn);

		
		ListView lv = getListView();
		lv.setTextFilterEnabled(true);
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				ann = annList.get(position);
				showDialog(DIALOG_YES_NO_LONG_MESSAGE);
			}

		});

	}

	/*
	 * Receive message successful or false from PostAnnouncementActivity and
	 * refresh announcement list
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == request_Code) {
			if (resultCode == RESULT_OK) {
				String result = data.getData().toString();
				if (result.equalsIgnoreCase("DONE!!!")) {

					Toast.makeText(getApplicationContext(),
							"Post Success.", Toast.LENGTH_LONG).show();

					annList = (ArrayList<Announcement>) ws
							.refreshAnnouncementList(courseScheduleId);
					aaAnn.notifyDataSetChanged();
					aaAnn = new ArrayAdapter<org.me.bean.Announcement>(
							getApplicationContext(), R.layout.courseinfor,
							annList);

					setListAdapter(aaAnn);

				} else {
					Toast.makeText(getApplicationContext(), "Post fault",
							Toast.LENGTH_SHORT).show();
				}

			} else if (resultCode == RESULT_CANCELED) {
				String result = data.getData().toString();
			}
		}

	}

	@Override
	protected Dialog onCreateDialog(int id) {
		// Announcement ann = args.getParcelable("Announcement");
		switch (id) {
		case DIALOG_YES_NO_LONG_MESSAGE:
			return new AlertDialog.Builder(AnnListActivity.this)
					.setIcon(R.drawable.icon)
					.setTitle(ann.getTitle())
					.setMessage(ann.getContent())
					.setPositiveButton("OK",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									Toast.makeText(getApplicationContext(),
											"this is click 1 " + whichButton,
											Toast.LENGTH_SHORT).show();
								}
							})
					.setNeutralButton("Delete",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {

									String result = ws.deleleAnnouncement(Integer
											.parseInt(ann.getAnnouncementId()));

									if (result.equalsIgnoreCase("true")) {
										Toast.makeText(
												getApplicationContext(),
												" Announcement "
														+ ann.getAnnouncementId()
														+ " was be deleted.",
												Toast.LENGTH_SHORT).show();

									} else {
										Toast.makeText(getApplicationContext(),
												"false", Toast.LENGTH_SHORT)
												.show();
									}

									refreshAnnouncements();

								}
							})
					.setNegativeButton("Cancel",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {

								}
							}).create();
		case (OtherDialog):

			LayoutInflater li = LayoutInflater.from(this);
			View annDetailView = li.inflate(R.layout.ann_detail, null);
			AlertDialog.Builder annDialog = new AlertDialog.Builder(this);
			annDialog.setTitle("Announcement");
			annDialog.setView(annDetailView);
			return annDialog.create();
		}
		return null;
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		// TODO Auto-generated method stub
		super.onPrepareDialog(id, dialog);
		switch (id) {
		case (DIALOG_YES_NO_LONG_MESSAGE):
			AlertDialog annDialog = (AlertDialog) dialog;
			annDialog.setTitle(ann.getTitle());
			annDialog.setMessage(ann.getContent());

			break;
		case (OtherDialog):
			AlertDialog othAnnDialog = (AlertDialog) dialog;
			othAnnDialog.setTitle(ann.getTitle());
			TextView tv = (TextView) findViewById(R.id.tv_anndetail);
			tv.setText(ann.getContent());
			break;

		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		CreateMenu(menu);
		return true;
	}

	private void CreateMenu(Menu menu) {
		menu.setQwertyMode(true);
		MenuItem mnu1 = menu.add(0, 0, 0, "Post Announcement");
		{
			mnu1.setAlphabeticShortcut('p');
			mnu1.setIcon(R.drawable.icon);
		}
		MenuItem mnu2 = menu.add(0, 1, 1, "Refresh");
		{
			mnu2.setAlphabeticShortcut('r');
			mnu2.setIcon(R.drawable.icon);
		}

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return MenuChoice(item);
	}

	private boolean MenuChoice(MenuItem item) {
		switch (item.getItemId()) {
		case 0:

			Intent i = new Intent(getApplicationContext(),
					PostAnnouncementActivity.class);
			i.putExtra("CourseScheduleId", courseScheduleId);
			i.putExtra("Instructor", inst);
			startActivityForResult(i, request_Code);

			return true;
		case 1:
			refreshAnnouncements();
			return true;

		}
		return false;
	}

	private void refreshAnnouncements() {
		// annList.clear();
		org.me.services.WSlib ws = new org.me.services.WSlib();
		annList = (ArrayList<Announcement>) ws
				.refreshAnnouncementList(courseScheduleId);
		aaAnn.notifyDataSetChanged();
		aaAnn = new ArrayAdapter<org.me.bean.Announcement>(
				getApplicationContext(), R.layout.courseinfor, annList);

		setListAdapter(aaAnn);

	}

}
